package com.zhangyu.shadow.repair.delegate.repair;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.zhangyu.shadow.repair.config.RepairItemType;
import com.zhangyu.shadow.repair.config.SpKey;
import com.zhangyu.shadow.repair.delegate.repair.entity.Level1Item;

import java.util.ArrayList;
import java.util.List;

import st.lib.base.storage.ShadowPreference;
import st.lib.base.ui.recycler.MultipleField;
import st.lib.base.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2018/3/27
 */

 class PostRepairData {

    /**
     * Id : 0
     * VehicleCode : 82215
     * ShiftGroup : {"Id":148}
     * ReportLocation : 可以不填
     * ReportTime : 可以不填
     * InFactoryTime : 可以不填
     * ListTrouble : [{"Classification":{"Id":1},"TroubleType":{"Id":6,"Code":"0201","Name":"发动机发吐"}},{"Classification":{"Id":2},"TroubleType":{"Id":7,"Code":"0201","Name":"车身抖"}}]
     */

    private int Id;
    private String VehicleCode;
    private ShiftGroupBean ShiftGroup;
    private String ReportLocation;
    private String ReportTime;
    private String InFactoryTime;
    private List<ListTroubleBean> ListTrouble;

    public int getId() {
        return Id;
    }

    public void setId(int Id) {
        this.Id = Id;
    }

    public String getVehicleCode() {
        return VehicleCode;
    }

    public void setVehicleCode(String VehicleCode) {
        this.VehicleCode = VehicleCode;
    }

    public ShiftGroupBean getShiftGroup() {
        return ShiftGroup;
    }

    public void setShiftGroup(ShiftGroupBean ShiftGroup) {
        this.ShiftGroup = ShiftGroup;
    }

    public String getReportLocation() {
        return ReportLocation;
    }

    public void setReportLocation(String ReportLocation) {
        this.ReportLocation = ReportLocation;
    }

    public String getReportTime() {
        return ReportTime;
    }

    public void setReportTime(String ReportTime) {
        this.ReportTime = ReportTime;
    }

    public String getInFactoryTime() {
        return InFactoryTime;
    }

    public void setInFactoryTime(String InFactoryTime) {
        this.InFactoryTime = InFactoryTime;
    }

    public List<ListTroubleBean> getListTrouble() {
        return ListTrouble;
    }

    public void setListTrouble(List<ListTroubleBean> ListTrouble) {
        this.ListTrouble = ListTrouble;
    }

    public static class ShiftGroupBean {
        /**
         * Id : 148
         */

        private int Id;

        public int getId() {
            return Id;
        }

        public ShiftGroupBean setId(int Id) {
            this.Id = Id;
            return this;
        }
    }

    public static class ListTroubleBean {
        /**
         * Classification : {"Id":1}
         * TroubleType : {"Id":6,"Code":"0201","Name":"发动机发吐"}
         */

        private ClassificationBean Classification;
        private TroubleTypeBean TroubleType;

        public ClassificationBean getClassification() {
            return Classification;
        }

        public void setClassification(ClassificationBean Classification) {
            this.Classification = Classification;
        }

        public TroubleTypeBean getTroubleType() {
            return TroubleType;
        }

        public void setTroubleType(TroubleTypeBean TroubleType) {
            this.TroubleType = TroubleType;
        }

        public static class ClassificationBean {
            /**
             * Id : 1
             */

            private String Code;

            public String getCode() {
                return Code;
            }

            public ClassificationBean setCode(String code) {
                this.Code = code;
                return this;
            }
        }

        public static class TroubleTypeBean {
            /**
             * Id : 6
             * Code : 0201
             * Name : 发动机发吐
             */

            private int Id;
            private String Code;
            private String Name;

            public int getId() {
                return Id;
            }

            public TroubleTypeBean setId(int Id) {
                this.Id = Id;
                return this;
            }

            public String getCode() {
                return Code;
            }

            public TroubleTypeBean setCode(String Code) {
                this.Code = Code;
                return this;
            }

            public String getName() {
                return Name;
            }

            public TroubleTypeBean setName(String Name) {
                this.Name = Name;
                return this;
            }
        }
    }

    public static String getJsonData(int id,
                                     String VehicleCode,
                                     String ReportTime,
                                     String InFactoryTime,
                                     ExpandableItemAdapter itemAdapter) {
        PostRepairData postRepairData = new PostRepairData();
        postRepairData.setId(id);
        postRepairData.setVehicleCode(VehicleCode);
        postRepairData.setShiftGroup(new ShiftGroupBean().
                setId(Integer.parseInt(ShadowPreference.getCustomAppProfile(SpKey.FACTORY_ID))));
        postRepairData.setReportLocation("");
        postRepairData.setReportTime(ReportTime);
        postRepairData.setInFactoryTime(InFactoryTime);
        List<ListTroubleBean> listTroubleBeans = new ArrayList<>();
        postRepairData.setListTrouble(listTroubleBeans);
        List<MultiItemEntity> entityList = itemAdapter.getData();
        int size = entityList.size();
        for (int i = 1; i < size; i += 2) {
            Level1Item level1Item = (Level1Item) entityList.get(i);
            List<MultipleItemEntity> itemEntities = level1Item.mMultiItemEntities;
            if (itemEntities != null) {
                for (MultipleItemEntity entity : itemEntities) {
                    ListTroubleBean troubleBean = new ListTroubleBean();
                    troubleBean.setClassification(
                            new ListTroubleBean.ClassificationBean().
                                    setCode((String) entity.getField((RepairItemType.ITEM_CLASSIFICATION_CODE))));
                    troubleBean.setTroubleType(new ListTroubleBean.TroubleTypeBean()
                            .setId((Integer) entity.getField(MultipleField.ID))
                            .setCode((String) entity.getField(RepairItemType.ITEM_CODE))
                            .setName((String) entity.getField(MultipleField.NAME)));
                    listTroubleBeans.add(troubleBean);
                }
            }
        }
        String jsonData = JSON.toJSONString(postRepairData);
        Log.e("data", jsonData);
        return jsonData;
    }
}
